package com.cyx;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author 陈裕星
 * @version 1.0
 */
public class 和为k的子数组 {
    //构造前缀和数组
    static int[] preSum;
    static int[] preSum2;
    public static void main(String[] args) {
        int nums[] = {1,1,1};
        int k = 2;
        preSum = new int[nums.length + 1];
        for(int i = 1; i < preSum.length; i++){
            preSum[i] = preSum[i-1] + nums[i-1];
        }
        System.out.println(Arrays.toString(preSum));
        int res = 0;
        for(int i = 1; i < preSum.length; i++){
            if(preSum[i] == k) res++;
            for(int j = i + 1; j < preSum.length; j++){
                if(preSum[j] - preSum[i] == k) res++;
            }
        }
        System.out.println(res);
        res = 0;
        //利用HashMap优化
        HashMap<Integer,Integer> map = new HashMap<>();

    }
}
